//
//  DDNewIntroViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/5/6.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDNewIntroViewController.h"

@interface DDNewIntroViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *coverImageNames;

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIButton *enterButton;

@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation DDNewIntroViewController

- (id)initWithCoverImageNames:(NSArray*)coverNames{
    
    if (self = [super init]) {
        [self initSelfWithCoverNames:coverNames];
    }
    return self;
}

- (void)initSelfWithCoverNames:(NSArray *)coverNames
{
    self.coverImageNames = [NSArray arrayWithArray:coverNames];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *bgImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"welcomeBg"]];
    bgImage.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    self.pageControl.numberOfPages = self.coverImageNames.count;
    
    [self.view addSubview:bgImage];
    [self.view addSubview: self.mainScrollView];
    [self.view addSubview:self.pageControl];
    
    self.mainScrollView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.mainScrollView.contentSize = CGSizeMake(kScreenWidth * self.coverImageNames.count, 0);

    
    for (int i = 0; i < [self.coverImageNames count]; i ++) {
        UIImageView *imgView = [[UIImageView alloc] init];
        
//        imgView.contentMode = UIViewContentModeScaleAspectFill;
        [imgView setImage:[UIImage imageNamed:self.coverImageNames[i]]];
        
        imgView.frame = CGRectMake(i *kScreenWidth, 0, kScreenWidth, kScreenHeight);
        
//        imgView.center = CGPointMake(i *kScreenWidth + kScreenWidth/2, kScreenHeight/2);
        [self.mainScrollView addSubview:imgView];
        
        if (i == [self.coverImageNames count] - 1) {
            
            [self.mainScrollView addSubview:self.enterButton];
            self.enterButton.frame = CGRectMake( i *kScreenWidth + (kScreenWidth-141)/2, kScreenHeight - 73, 141, 38);
        }
    }
}

#pragma mark
#pragma mark - Event Response

//点击进入主视图
- (void)onEnterButtonClick:(UIButton *)button
{
//    if (self.didSelectedEnter) {
//        self.didSelectedEnter();
//    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_GUIDERIMGVIEW_END object:nil];
    
}

- (void)changePage:(UIPageControl *)pageControll
{
    NSInteger page = pageControll.currentPage;
    
    [self.mainScrollView setContentOffset:CGPointMake(kScreenWidth *page, 0)];
}

#pragma mark
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    
    int page = round(sender.contentOffset.x / kScreenWidth);
    
    self.pageControl.currentPage = page;
}

#pragma mark
#pragma mark - Getters and Setters

- (UIScrollView *)mainScrollView
{
    if (_mainScrollView == nil) {
        _mainScrollView = [[UIScrollView alloc] init];
        _mainScrollView.delegate = self;
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.backgroundColor = [UIColor clearColor];
    }
    
    return _mainScrollView;
}

- (UIButton *)enterButton
{
    if (_enterButton == nil) {
        _enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_enterButton setBackgroundImage:[UIImage imageNamed:@"welcome_enter"] forState:UIControlStateNormal];
        [_enterButton addTarget:self action:@selector(onEnterButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        WEAKSELF
        [[_enterButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) {
            [weakSelf onEnterButtonClick:weakSelf.enterButton];
        }];
    }
    
    return _enterButton;
}

- (UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPage = 0;
        _pageControl.userInteractionEnabled = YES;
        _pageControl.frame = CGRectMake((kScreenWidth- 150)/2, kScreenHeight - 30, 150, 20);
        [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    }
    
    return _pageControl;
}

@end
